//
//  CouponDetailVC.m
//  HengYouCai
//
//  Created by ycgwl on 2017/5/3.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "CouponDetailVC.h"
#import "RedPackModel.h"
#import "BannerWebViewVC.h"
#import "HengYouCai-Swift.h"

@interface CouponDetailVC ()
{
    BOOL _isLoseEfficacy;
    NSInteger _pageNo;
}
@property (nonatomic, retain)NSMutableArray *totalArr;
@property (weak, nonatomic) IBOutlet UILabel *tishiLab;

/**
 可使用 已过期
 */
@property (nonatomic, copy)NSString *status;

@end

@implementation CouponDetailVC

- (void)dealloc{
    NSLog(@"CouponDetailVC dealloc");
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    _isLoseEfficacy = YES;
    _status = @"ksy";
    _pageNo = 1;
    self.view.backgroundColor = Normal_BackColor;
    [self.view showHUDLoading];
    [self netWorking];

    [self startRefresh];
    
    self.tableView.mj_footer.hidden = YES;

}
//- (void)updateViewConstraints
//{
//    [super updateViewConstraints];
//    _leftValue.constant = f_i6real(140);
//    
//}
#pragma mark- 刷新
- (void)startRefresh
{
    __weak CouponDetailVC * weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        CouponDetailVC * strongSelf = weakSelf;
        strongSelf ->_pageNo = 1;
        [strongSelf netWorking];
        
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        CouponDetailVC * strongSelf = weakSelf;
        strongSelf -> _pageNo++;
        [strongSelf netWorking];
    }];
    
}
#pragma tableviewDatasource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    if (_tishiLab) {
//        
//        if (_isLoseEfficacy) {
//            _tishiLab.text = [_type isEqualToString:@"0"]?@"已无更多失效红包":@"已无更多失效加息券";
//        }else{
//            _tishiLab.text = [_type isEqualToString:@"0"]?@"已无更多可用红包":@"已无更多可用加息券";
//        }
//    }

    return _totalArr.count;
}
#pragma tableviewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZYReturnRedbagVCCell * cell = [ZYReturnRedbagVCCell wl_createCellWithTableView:tableView];
    [cell wl_CouponconfigurationCellWithModelTemp:self.totalArr listView:tableView indexPath:indexPath];
//    [cell wl_CouponConfigurationCellWithModelTemp:self.totalArr listView:tableView indexPath:indexPath];
//    RedPackCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CouponDetailCellID"];
//    cell.type = _type;
//    cell.isLoseEfficacy = _isLoseEfficacy;
//    cell.model = _totalArr[indexPath.row];
    return cell;
}

/**
 已失效 可使用
 */
- (IBAction)isEfficacy:(UIButton *)sender {
    // 防止多次点击
    [self.view showHUDLoading];
    _pageNo=1;
    _isLoseEfficacy = !_isLoseEfficacy;
    

    [sender setTitle:_isLoseEfficacy?@"查看可使用":@"查看已失效" forState:UIControlStateNormal];
    [self netWorking];
}
- (NSMutableArray *)totalArr
{
    if (!_totalArr) {
        _totalArr = [NSMutableArray array];
    }
    return _totalArr;
}
- (void)setType:(NSString *)type
{
    _type = type;

}
- (void)netWorking
{
    
    WLGetWeakSelf(weakSelf, self);
    NSDictionary *dic = @{@"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],
                          @"pageSize":@"10",
                          @"type":_type,
                          @"flag":_isLoseEfficacy?@"ysx":@"ksy"};
    [HYCNetService myWalletMyredPackListWithDict:dic onSuccess:^(NSDictionary *dictResult) {
        //
        [weakSelf.view hideHUDLoading:0];
        NSArray *lists = dictResult[@"myredPackList"];
        if (_pageNo==1) {
            [_totalArr removeAllObjects];
        }
        
        if (lists.count < 10) {
            _tishiLab.text = _isLoseEfficacy?([_type isEqualToString:@"0"]?@"已无更多失效红包":@"已无更多失效加息券"):([_type isEqualToString:@"0"]?@"已无更多可用红包":@"已无更多可用加息券");

        }else{
            _tishiLab.text = @"";
        }
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        
        weakSelf.tableView.mj_footer.hidden = lists.count<10;
        
        [weakSelf.totalArr addObjectsFromArray:[RedPackModel mj_objectArrayWithKeyValuesArray:lists]];

        [weakSelf.tableView reloadData];
    } andFailed:^(NSInteger code, NSString *errorMsg) {
        
        [weakSelf.view showHUDFailTips:errorMsg hideDelay:kHUDHideDelay];

        _pageNo--;
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}
@end
